package com.kahluastudios.mealfinder.desktop.client.recommendations;

import java.util.List;

import com.google.gwt.cell.client.ImageCell;
import com.google.gwt.cell.client.SafeHtmlCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.SimplePager.TextLocation;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.view.client.ListDataProvider;
import com.kahluastudios.mealfinder.desktop.shared.PromotionalCode;
import com.kahluastudios.mealfinder.desktop.shared.RecommendationVO;
import com.kahluastudios.mealfinder.desktop.shared.RestaurantInfoVO;

public class RecommendationsList extends FlowPanel {
	
	private CellTable<RecommendationVO> contentTable;
	private ListDataProvider<RecommendationVO> dataProvider;
	private SimplePager pager;
	
	public RecommendationsList() {
		this.contentTable = new CellTable<RecommendationVO>(new RecommendationVOKeyProvider());
		this.contentTable.setWidth("100%",true);
		this.contentTable.setAutoHeaderRefreshDisabled(true);
		this.contentTable.setAutoFooterRefreshDisabled(true);		
		
		this.dataProvider = new ListDataProvider<RecommendationVO>();
		this.dataProvider.addDataDisplay(this.contentTable);
		ListHandler<RecommendationVO> sortHandler = 
				new ListHandler<RecommendationVO>(this.dataProvider.getList());
		this.contentTable.addColumnSortHandler(sortHandler);
		
	    SimplePager.Resources pagerResources = GWT.create(SimplePager.Resources.class);
	    this.pager = new SimplePager(TextLocation.CENTER, pagerResources, false, 0, true);
	    this.pager.setDisplay(this.contentTable);	    
		
		Column <RecommendationVO, SafeHtml> foodTypeColumn=
				new Column<RecommendationVO, SafeHtml>(new SafeHtmlCell()) {
					@Override
					public SafeHtml getValue(RecommendationVO object) {
						SafeHtmlBuilder builder = new SafeHtmlBuilder();
						builder.appendHtmlConstant("<b>" + object.getRestaurantInfo().getCuisine()+"</b>"); 
						return builder.toSafeHtml();
					}
		};
		this.contentTable.addColumn(foodTypeColumn, "Cuisine");
				
		Column <RecommendationVO, SafeHtml> address =
				new Column<RecommendationVO, SafeHtml>(new SafeHtmlCell()) {
					@Override
					public SafeHtml getValue(RecommendationVO object) {						
						SafeHtmlBuilder builder = new SafeHtmlBuilder();
						
						RestaurantInfoVO restaurantInfo = new RestaurantInfoVO();
						
						builder.appendHtmlConstant("<b>" + restaurantInfo.getName()+"</b>");
						builder.appendHtmlConstant("<br/>");
						builder.appendHtmlConstant("<i><b>Address: " + restaurantInfo.getAddress()+"</b></i>");
						
						return builder.toSafeHtml();
					}
		};
		this.contentTable.addColumn(address, "Name");
		
	    Column<RecommendationVO, String> imageColumn= 
    			new Column<RecommendationVO, String>(new ImageCell()) {
				@Override
				public String getValue(RecommendationVO viewObject) {
					return viewObject.getRestaurantInfo().getEncodedRestaurantImage();
				}
	    };
	    this.contentTable.addColumn(imageColumn, "      ");
		
		
		Column <RecommendationVO, SafeHtml> topOffers =				
				new Column<RecommendationVO, SafeHtml>(new SafeHtmlCell()) {
					@Override
					public SafeHtml getValue(RecommendationVO object) {						
						SafeHtmlBuilder builder = new SafeHtmlBuilder();
						
						List<PromotionalCode> codes = object.getPromoCodes();
						int displayed=0;
						for (PromotionalCode promotionalCode : codes) {
							builder.appendHtmlConstant(promotionalCode.getBannerLine());							
							displayed++;
							if (displayed==3) 
								break;
							else builder.appendHtmlConstant("<br/>");
						}
						
						if (displayed>=3 && codes.size()>displayed) {
							int remaining =  codes.size() - displayed;
							builder.appendHtmlConstant("<br/> <b><i>......." + remaining + " more...</i></b>"); 
						}
						
						return builder.toSafeHtml();
					}
		};
		this.contentTable.addColumn(topOffers, "Offers");
				
		add(this.contentTable);
	}
	
	public ListDataProvider<RecommendationVO> getDataProvider() 
	{ return this.dataProvider; }
}//end class def